unit clsContratante;

interface

uses dbtables,dm,biblio,menu,SysUtils;

type
  TContratante=Class(TObject)
   public
      cgc         : string;
      razao       : String;
      dias_aviso  : String;
      ultima_senha: string;
      dia_fixo    : string;
      meses       : string;
      data_atual  : tdatetime;
      data_exp    : tdatetime;
      checkSum    : Double;
      function Gravar(xcgc:String)  : boolean;
      function Recuperar            : boolean;
      function Excluir(xcgc:String) : boolean;
      function Verifica_Existe      : boolean;
   end;

implementation

uses DB;

function TContratante.Gravar;
var tq: TQuery;
begin
 dmint.db.StartTransaction;
 try
  tq             := Tquery.Create(dmint);
  tq.databasename:= dmint.db.databasename;
  tq.active      := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Contratante where cgc=:a');
  tq.params[0].asstring:= xcgc;
  tq.open;
  if tq.eof then begin
   tq.sql.clear;
   tq.params.clear;
   tq.sql.add('Insert into Contratante values (:a,:b,:c,:d,:e,:f,:g,:h,:i)');
  end else begin
   tq.sql.clear;
   tq.params.clear;
   tq.sql.add('Update Contratante set razao=:b,dias_aviso=:c,ultima_senha=:d,dia_fixo=:e,'+
              'meses=:f,data_atual=:g,data_exp=:h,checkSum=:i where cgc=:a');
  end;
  tq.parambyname('a').asstring  := xcgc;
  tq.parambyname('b').asstring  := razao;
  tq.parambyname('c').asstring  := dias_aviso;
  tq.parambyname('d').asstring  := ultima_senha;
  tq.parambyname('e').asstring  := dia_fixo;
  tq.parambyname('f').asstring  := meses;
  tq.parambyname('g').asdatetime:= data_atual;
  tq.parambyname('h').asdatetime:= data_exp;
  tq.parambyname('i').AsFloat   := checkSum;
  tq.close;
  tq.execsql;
  tq.Close;
  tq.Free;
  dmint.db.Commit;
  result:= true;
 except
  dmint.db.Rollback;
  result:= false;
 end;
end;

function TContratante.Recuperar;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from Contratante');
 tq.open;
 if tq.eof then result:= false else result:= true;
 cgc         := tq.FieldbyName('cgc').AsString;
 razao       := tq.FieldbyName('razao').AsString;
 dias_aviso  := tq.FieldbyName('dias_aviso').AsString;
 ultima_senha:= tq.FieldbyName('ultima_senha').AsString;
 dia_fixo    := tq.FieldbyName('dia_fixo').AsString;
 meses       := tq.FieldbyName('meses').AsString;
 data_atual  := tq.FieldbyName('data_atual').Asdatetime;
 data_exp    := tq.FieldbyName('data_exp').Asdatetime;
 checkSum    := tq.fieldbyname('checkSum').AsFloat;
 tq.close;
 tq.free;
end;

function TContratante.Excluir;
var tq: TQuery;
begin
 dmint.db.StartTransaction;
 try
  tq             := Tquery.Create(dmint);
  tq.databasename:= dmint.db.databasename;
  tq.active      := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from Contratante where cgc=:a');
  tq.params[0].asstring:= xcgc;
  tq.ExecSQL;
  tq.close;
  tq.Free;
  dmint.db.Commit;
  result:= true;
 except
  dmint.db.Rollback;
  result:= false;
 end;
end;

function TContratante.Verifica_Existe;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.Params.Clear;
 tq.sql.add('Select * from Contratante');
 tq.Open;
 if tq.eof then result:= false else result:= true;
 tq.close;
 tq.free;
end;

end.
